package com.eastshop.user.dao;

import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.eastshop.user.vo.User;
import com.eastshop.utils.PageHibernateCallback;

/**
 * 用户模块的DAO类
 * @author admin
 *
 */
public class UserDao extends HibernateDaoSupport{

	// Dao层用户登录的方法
	public User find(User user) {
		String hql = "from User where username = ? and password = ? and state = ?";
		List<User> list = this.getHibernateTemplate().find(hql,
				user.getUsername(), user.getPassword(), 1);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	// Dao层根据用户名查找用户
	public User findByUsername(String username) {
		String hql = "from User where username = ?";
		List<User> list = this.getHibernateTemplate().find(hql, username);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}
	
	// Dao层保存用户的信息
	public void save(User user) {
		this.getHibernateTemplate().save(user);
	}

	// Dao层根据激活码查找用户
	public User findByCode(String code) {
		String hql = "from User where code = ?";
		List<User> list = this.getHibernateTemplate().find(hql, code);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	// Dao层更新用户的信息
	public void update(User user) {
		this.getHibernateTemplate().update(user);
	}

	// Dao层查询用户的个数
	public int findCount() {
		String hql = "select count(*) from User";
		List<Long> list = this.getHibernateTemplate().find(hql);
		if (list != null && list.size() > 0) {
			return list.get(0).intValue();
		}
		return 0;
	}

	// Dao层分页查询用户信息
	public List<User> findAllWithPage(int begin, int limit) {
		String hql = "from User";
		List<User> list = this.getHibernateTemplate().execute(new PageHibernateCallback<User>(hql, null, begin, limit));
		if (list != null && list.size() > 0) {
			return list;
		}
		return null;
	}

	// Dao层根据用户id查询用户
	public User findByUid(Integer uid) {
		User user = this.getHibernateTemplate().get(User.class, uid);
		return user;
	}

	// Dao层删除用户的方法
	public void delete(User currUser) {
		this.getHibernateTemplate().delete(currUser);
	}

}
